#coding=utf-8
from cassandra.cluster import Cluster
import sys
import string
import random

# http://datastax.github.io/python-driver/getting_started.html
"""
Note that you should use %s for all types of arguments, not just strings. For example, this would be wrong:

session.execute("INSERT INTO USERS (name, age) VALUES (%s, %d)", ("bob", 42))  # wrong
"""

def test():
    session.execute('insert into t (k, page, v, year) values (%s, %s, %s, %s)', ('k1', 100, 'v1', '2017'))
    session.execute('insert into t (k, page, v, year) values (%s, %s, %s, %s)', ['k2', 100, 'v2', '2017'])
    session.execute('update t set page = %s, v = %s, year = %s where k = %s', [100, 'v2', '2017', 'k3'])


def verify(s, n):
    for i in range(0, n):
        rows = s.execute('select * from t;')# where k=' + str(i) + ';')
        for r in rows:
            print(r.v, r.page)

def generate_random_str(n):
    _str = ''
    for i in range(n):
        _str += str(random.choice(string.ascii_lowercase + string.digits))

    return _str

def do_insert(s, n):
    #max_size = (1 << 10) * 10 # 10k
    for i in range(n):
        #s.execute('insert into t(k, v, v2) values(%s, %s, %s)', (str(i), str(i) * 5120, str(i) * 5120))
        session.execute('insert into t (k,v,v2) values(%s, %s, %s)', ('0','0'*2,'0'*3))

if __name__ == '__main__':
    if len(sys.argv) != 1:
        print("arg error")
        sys.exit(0)

    host = '10.153.185.111'
    ks = 'keyspace1'

    #host2 = '10.77.44.158'
    #ks2 = 'webcrawl'

    c = Cluster([host])
    session = c.connect()
    #session.execute("create keyspace %s with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" % keyspacename)
    session.set_keyspace(ks)

    session.execute('drop table t')
    session.execute('create table t (k text primary key, v text, v2 text)')
    session.default_consistency_level = 'ONE'

    #session.execute('insert into t (k, page, v, year) values (%s, %s, %s, %s)', ('k1', 100, 'v1', '2019'))
    #session.execute('insert into t (k,v,v2) values(%s, %s, %s)', (0,0,0))

    do_insert(session, 3)
    #verify(session, 10)

    c.shutdown()
    print('done')
